
//token 就10分钟有效期
let activity_tag = 'ysf_sign'
let request_log = ""
let request_key = activity_tag + '_award'
let skill_count_key = activity_tag + '_award_count'
let request_log_key = activity_tag + '_award_log'
let skill_name = 'skill_name'
let skill_result = false
let default_count = 5
function rewrite_body(body) {
    console.log(body);
    if (body.subCodeMsg) {
        body.subCodeMsg = "成功抓到请求";
    }
}

function resp_case(request_value, count, res) {
    console.log('请求结果：');
    console.log(res.headers.date);
    console.log(res.data);
    try {
        if (res.data.description && res.data.description.includes("我的-奖励")) {
            skill_result = true
            request_log = request_log + res.headers.date + ":抢购成功\n"
            $store.set(request_log_key, request_log)
        } else {
            axios_skill(request_value, count)
        }
    } catch (err) {
        axios_skill(request_value, count)
    }
    // if(res.data.respCd=="01"){
    //     skill_result = true

    // }
    // else if(res.data.subCodeMsg.includes("已抢完")){
    //     $feed.push(skill_name+"秒杀结果：", res.data.subCodeMsg)
    // }

    if (!request_log.includes(res.data.message)) {
        request_log = request_log + res.headers.date + ":" + res.data.message + "\n"
        $store.set(request_log_key, request_log)
    }

}
function error_case(request_value, count, e) {
    console.log(e.message)
    resp_case(request_value, count, e.response)

    //axios_skill(request_value,count)
}

//上面是暂时需要适配的部分
!(async () => {
    if (typeof $request !== "undefined") {
        console.log(`开始获取请求参数`);
        get_request_key();
        let body = $response.body;
        // body =  JSON.parse(body);
        // rewrite_body(body);
        // body =  JSON.stringify(body);
        $done({ body })
    } else {
        console.log(`开始执行请求`);
        request_log = $store.get(skill_name) + '\n';
        $store.set(request_log_key, request_log)
        let request_value = $store.get(request_key);
        if (request_value && request_value != "") {
            //  $store.delete没有暴露出来，无法使用，兼容下
            // $store.set(request_key, "")  
            setTimeout(axios_skill, 700, request_value, default_count);
            // axios_skill(request_value,count);

        } else {
            console.log(request_key + `不存在`);
        }
    }
})()
    .catch((e) => console.log(e))
    .finally(() => $done())

function axios_skill(request_value, count) {
    if (!skill_result && count > 0) {
        console.log(`执行请求：`);
        count--;
        $axios(request_value)
            .then(res => resp_case(request_value, count, res))
            .catch(e => error_case(request_value, count, e));
    }

}


function get_request_key() {
    $store.set(request_key, $request)
}
